<template>
  <div class="root">
    <div class="header">
      <el-button
        @click="dialogFormVisible = true"
        type="primary"
        icon="el-icon-plus"
        >添加</el-button
      >
    </div>
    <div class="table">
      <template>
        <el-table :data="trademarkList" border style="width: 100%">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="tmName" label="品牌名称" width="180">
          </el-table-column>
          <el-table-column label="品牌Logo">
            <template slot-scope="{ row, $index }">
              <img :src="row.logoUrl" alt="" style="width:100px;height=100px" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="warning"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 7]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form style="width: 80%" :rules="rules" :model="form" ref="ruleForm">
        <el-form-item label="活动名称" prop="tmName" :label-width="formLabelWidth">
          <el-input v-model="form.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品logo" prop="logoUrl" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitGoodsMsg">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      trademarkList: [],
      currentPage: 1, //当前页
      pageLimit: 3, //每页的数量
      total: 0, //商品总数
      form: {
        tmName: "",
        logoUrl: "",
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      rules: {
        //添加表单验证规则
        tmName: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        logoUrl: [
          { required: true, message: "请上传商品logo", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    goodIndex() {
      return (this.currentPage - 1) * this.pageLimit;
    },
  },
  mounted() {
    this.getTrademarkList(1, 3);
  },
  methods: {
    async getTrademarkList(page, limit) {
      let res = await this.$API.trademark.reqTrademarkList(page, limit);
      if (res.code == 200) {
        this.trademarkList = res.data.records;
        this.total = res.data.total;
      }
    },
    handleSizeChange(val) {
      this.pageLimit = val;
      this.getTrademarkList(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTrademarkList(val, this.pageLimit);
    },
    //修改数据
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form = { ...row }; //对象浅拷贝
    },
    //删除数据
    async handleDelete(index, row) {
      let res=await this.$API.trademark.reqDeleteTradeMark(row.id);
      if(res.code==200){
        this.getTrademarkList(this.currentPage,this.pageLimit)
        this.$message.success("删除成功")
      }
      else{
        this.$message.error("删除失败");
      }
    },

    submitGoodsMsg(id) {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) {
          return;
        }
        let res = await this.$API.trademark.reqAddOrUpdateTrademark(this.form);
        if (res.code == 200) {
          this.dialogFormVisible = false;
          this.$message.success(id ? "修改成功" : "添加成功");
        } else {
          this.$message.error(id ? "修改成功" : "添加失败");
        }
        this.form = {};
        this.getTrademarkList(this.currentPage,this.pageLimit)
      });
    },

    handleAvatarSuccess(res, file) {
      console.log(res);
      console.log(file);
      this.form.logoUrl = file.response.data; //多种写法
      // this.form.logoUrl=res.data;
      // this.form.logoUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
.header {
  margin: 10px 0px;
}
.table {
  margin: 10px 0px;
}
.el-pagination {
  float: right;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>